﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HomeCareLibrary.DataAccess;
using HomeCareLibrary.Repository;
using HomeCareLibrary.Core.Dtos;
using HomeCareLibrary.Core.DataContracts;
using HomeCareLibrary.AppService.Mappers;

namespace HomeCareLibrary.AppService
{

    public class UserAppService : AppServiceBase
    {
        private HomeWizardServicesEntities context = new HomeWizardServicesEntities();
        public User_IDDto FindUser(int userId)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var repository = new Repository<User_ID>(unitOfWork);
                var user = repository.FirstOrDefault(x => x.user_id1 == userId);
                return user != null ? user.ToDTO() : null;
            }
        }

        public List<CTUpdateUserNamePasswordDto> UpdateUserNamePassword(UpdateUserNamePasswordRequest request)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var repository = new Repository<CTUpdateUserNamePassword>(unitOfWork);
                var queryParameter = string.Format("@paramUserId={0},@paramUserName='{1}',@paramPassword='{2}'", request.UserId, request.UserName, request.Password);
                var user = repository.ExecuteFuntion("[sp_Update_UsernamePassword]", queryParameter);
                return user.ToDTOs();
            }
        }

        /// <summary>
        /// get top most homenumber
        /// </summary>
        /// <param name="userId">user id</param>
        /// <returns></returns>
        public int GetHomeNumber(int userId)
        {
            return context.User_Details.Where(w => w.user_id == userId).OrderBy(o => o.home_number).Select(s => s.home_number).FirstOrDefault();
        }

        /// <summary>
        /// Update welcome_image and welcome_image_chg_time
        /// </summary>
        /// <param name="userId">userid</param>
        public void UpdateWelcomeImage(int userId, string welcomeImage, string welcomeImageChgTime)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var repository = new Repository<User_ID>(unitOfWork);
                var user =  repository.FirstOrDefault(w=>w.user_id1 == userId);
                if (user != null)
                {
                    user.welcome_image = welcomeImage;
                    user.welcome_image_chg_time = welcomeImageChgTime;
                }
                unitOfWork.SaveChanges();
            }
        }

        /// <summary>
        /// Get WelcomeImage 
        /// </summary>
        /// <param name="userId">userId</param>
        /// <returns></returns>
        public string GetWelcomeImage(int userId)
        {
            string welcomeImage = string.Empty;
            using (var unitOfWork = new UnitOfWork())
            {
                var repository = new Repository<User_ID>(unitOfWork);
                welcomeImage = repository.FirstOrDefault(w => w.user_id1 == userId).welcome_image;                
            }
            return welcomeImage;
        }

        /// <summary>
        /// Delete WelcomeImage
        /// </summary>
        /// <param name="userId">userId</param>
        public void DeleteWelcomeImage(int userId, string welcomeImageChgTime)
        {
            using (var unitOfWork = new UnitOfWork())
            {
                var repository = new Repository<User_ID>(unitOfWork);
                var user = repository.FirstOrDefault(w => w.user_id1 == userId);
                if (user != null)
                {
                    user.welcome_image = string.Empty;
                    user.welcome_image_chg_time = welcomeImageChgTime;
                }
                unitOfWork.SaveChanges();
            }
        }

    }
}
